<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 放置一个el-tree组件 -->
        <el-tree
          :props="{label: 'name',children: 'children'}"
          :data="list"
          :default-expand-all="true"
        >

          <el-row
            slot-scope="{ data }"
            type="flex"
            justify="space-between"
            align="middle"
            style="height: 40px; width: 100%;"
          >
            <el-col :span="20">
              <span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <el-col>
                  <!-- 下拉菜单 element -->
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <!-- 下拉菜单 -->
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-tree>
      </el-card>

    </div>

    <el-dialog :title="cTitle" :visible="showDialog" :show-close="false">
      <add-depart
        v-if="showDialog"
        :edit-id="editId"
        :add-id="addId"
        @close="showDialog=false"
        @update-depart="loadDepartments"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { arryToTree } from '@/utils/index.js'
import AddDepart from './addDepart'
export default {
  components: {
    AddDepart
  },
  data() {
    return {
      editId: null, // 当前要编辑的id
      curData: null,
      showDialog: false,
      // 依赖一份树形数据
      list: [{
        name: '财务部',
        children: [
          {
            name: '财务核算部',
            manager: 'aa',
            children: [{ name: '国内' }, { name: '国外' }]
          },
          {
            name: '税务核算部'
          }
        ]
      }]
    }
  },
  computed: {
    cTitle() {
      return this.editId ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const data = await getDepartments()
      console.log(data)
      this.list = arryToTree(data.depts)
    },
    hEdit(id) {
      this.addId = ''
      this.showDialog = true
      this.editId = id
    },
    hAdd(id) {
      this.editId = ''
      this.showDialog = true
      this.addId = id
    },
    hDel(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        console.log('aa')
        // 删除操作成功
        await delDepartment(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.hGetDepartments()
      }).catch(error => {
        console.log(error)
      })
    }
  }
}
</script>

<style>

</style>
